{* $Id$ *}
{include_core file="admin/lib/header.html" title="维修列表"}
<style>
  .goods-info{
    display:block;
    float:left;
    margin-left:5px;
    line-height:20px;
    width:65%;
  }
  .goods-info p{
    margin-top: 5px;
    height:16px;
    line-height: 16px;
    padding-left: 5px;
    padding-right: 5px;
    brefund:1px solid #EDEDED;
    background:#EFEFEF;
    display:inline-block;
  }
  .goods-table td{
    brefund:1px solid #EDEDED;
  }
  .goods-info a:first-child{
    color:#333;
  }
  .goods-info a:nth-child(2){
    color:#05A;
  }
  .goods-info a:hover{
    color:#23a18b;
  }
  .js_refundTable td{
    padding-top:10px;
    padding-bottom:10px;
  }
</style>
{$refund_status_list = repair_config::$repair_status_list}
<div class="page-header border-bottom clearfix">
  <h2 class="left">维修管理<span>在这里对维修进行管理</span></h2>
</div>
<div class="search-level1 clearfix" style="display:none">
  <form action="repair/admin" method="get">
    <input type="hidden" name="is_advenced" value="0">
    <input type="hidden" name="status" value="{$status}">
    <input {if $is_advenced == 1}style="display:none;"{/if} type="text" class="left" name="search_filter[refund_code]" value="{$search_filter.refund_code}" placeholder="输入维修号搜索">
    <button {if $is_advenced == 1}style="display:none;"{/if} type="submit" class="btn btn-success btn-sm left">搜索</button>
    {*<a href="javascript:void(0);" class="close js_advanced {if $is_advenced == 1}js_isOpen open{/if}">高级搜索<em></em></a>*}
  </form>
</div>
<div class="search-form js_advancedForm" {if $is_advenced == 0}style="display:none;"{/if}>
<form action="repair/admin" method="get" class="js_refundSearchForm">
  <input type="hidden" name="is_advenced" value="1">
  <input type="hidden" name="status" value="{$status}">
  <p>
    <label for="" class="search-f-label">维修单号：</label>
    <input type="text" class="search-f-ipt1" name="search_filter[refund_id]" value="{$search_filter.refund_id}">
  </p>
  <p>
    <label for="" class="search-f-label">订单号：</label>
    <input type="text" class="search-f-ipt1" name="search_filter[order_id]" value="{$search_filter.order_id}">
  </p>
  <p>
    <label for="" class="search-f-label">商品标题：</label>
    <input type="text" class="search-f-ipt1" name="search_filter[goods_title]" value="{$search_filter.goods_title}">
  </p>
  <p>
    <label for="" class="search-f-label">用户名称：</label>
    <input type="text" class="search-f-ipt1" name="search_filter[user_name]" value="{$search_filter.user_name}">
  </p>
  <p>
    <label for="" class="search-f-label">维修状态：</label>
    <select class="search-f-slt1" name="search_filter[refund_status]">
      <option value="">全部</option>
      {foreach $refund_status_list as $k=>$v}
      <option value="{$k}" {if $search_filter.refund_status == $k}selected{/if}>{$v}</option>
      {/foreach}
    </select>
  </p>
  <p>
    <label for="" class="search-f-label">添加时间：</label>
    <input type="text" class="search-f-ipt1 search-f-ipt2" name="search_filter[start_date]" value="{$search_filter.start_date}" id="startDate">
    <span class="search-f-line"></span>
    <input type="text" class="search-f-ipt1 search-f-ipt2" name="search_filter[end_date]" value="{$search_filter.end_date}" id="stopDate">
  </p>
  <p><label for="" class="search-f-label"></label><button type="submit" class="btn btn-success btn-xs js_searchBtn">搜索</button></p>
</form>
</div>
<!--start:列表-->
<table class="list-table goods-table">
  <caption>按状态：
    <a href='{AnUrl("repair/admin")}' {if !$refund_status && $status != '' && !$search_filter.refund_status} class="label label-primary" {else} class="label label-default" {/if}>全部</a>
    {foreach $refund_status_list as $k=>$v}
    <a href='{AnUrl("repair/admin?refund_status=$k")}' {if $refund_status == $k || $search_filter.refund_status == $k} class="label label-primary" {else} class="label label-default" {/if}>{$v}</a>
    {/foreach}
  </caption>
  <thead>
  <tr>
    {if $status == 1}
    <th style="width:20px;"><input type="checkbox" class="selectAll"></th>
    {/if}
    <th>维修单号</th>
    <th>维修状态</th>
    <th>订单编号</th>
    <th>订单状态</th>
    <th>维修用户昵称</th>
    <th>退款总金额</th>
    <th>商家备注</th>
    <th>详细信息</th>
    <th>操作</th>
  </tr>
  </thead>
  <tbody>
  {foreach $refund_list as $k=>$v}
  <tr data-id="{$v.id}">
    {if $status == 1}
    <td><input type="checkbox" class="listSelect"></td>
    {/if}
    <td>{order_helper::order_id_encode($v.id, 2)}</td>
    <td><span class="label {if $v.refund_status == 1}label-warning{elseif $v.refund_status == 5}label-primary{elseif $v.refund_status == 10}label-success{elseif $v.refund_status == 15}label-info{elseif $v.refund_status == 20}label-default{/if}">{$refund_status_list.{$v.refund_status}}</span></td>
    <td>{order_helper::order_id_encode($v.order_id)}</td>
    {$order_status = order_helper::get_order_status($v.order_id)}
    <td>{order_config::$dealer_progress.$order_status}</td>
    {$user_nick_name = user_helper::get_nick_name($v.user_id)}
    <td>{$user_nick_name}</td>
    <td class="js_refundTotalPrice_{$v.id}">
      {if $v.is_modify_price}{$v.real_price}{else}{$v.refund_price}{/if}
    </td>
    <td>{$v.note}</td>
    <td><span class="btn btn-xs btn-primary refund_info">详细信息</span></td>
    <td>
      {if $v.refund_status == 1}
      {if $order_status == 1 || $order_status == 10}
      <span class="btn btn-xs btn-success">退款</span>
      {elseif $order_status == 20 || $order_status == 15}
      <span class="btn btn-xs btn-primary refundOk">申请通过</span>
      <span class="btn btn-xs btn-danger refundCancel">申请驳回</span>
      {/if}
      {elseif ($v.refund_status >= 5 && $v.refund_status < 20) && $order_status == 20 || $order_status == 15}
      <span class="btn btn-xs btn-success refundOver">维修完成</span>
      {/if}
    </td>
  </tr>
  {foreachelse}
  <tr><td class="txtleft" colspan="10">暂无数据</td></tr>
  {/foreach}
  </tbody>
</table>
<!-- 遮罩 -->
<div class="float-bg" style="z-index: 19999; display: none"></div>
{include_core file="admin/lib/pager.html"}
<!--end:列表-->
{load_js file='zclip/jquery.zclip.min.js'}
{load_js file='zclip/ZeroClipboard.js'}
<script>
var resName = 'refund';
var jsrefundId = "{$search_filter.refund_id}";
$('.js_refundId').load(siteUrl+'/repair/load_refund_option?res_name='+resName,function(){
  $('.js_refundId').find('option').each(function(){
    if ($(this).attr('value') == jsrefundId) {
      $(this).attr('selected','selected');
    }
  });
});
$(function(){
  // 高级搜索
  $('.js_advanced').click(function(e){
    e.preventDefault();
    var thisObj = $(this);
    var outBox  = thisObj.closest('form');
    if (thisObj.hasClass('js_isOpen')) {
      thisObj.removeClass('js_isOpen open');
      outBox.find('input').show();
      outBox.find('button').show();
      $('.js_advancedForm').hide();
    } else {
      thisObj.addClass('js_isOpen open');
      outBox.find('input').hide();
      outBox.find('button').hide();
      $('.js_advancedForm').show();
    }
  });

  // 高级搜索时间验证
  $('.js_searchBtn').click(function(e){
    e.preventDefault();
    var thisObj   = $(this);
    var startDate = $.trim($('#startDate').val());
    var stopDate  = $.trim($('#stopDate').val());
    if (startDate > stopDate) {
      showMsg('结束时间应大于等于开始时间');
      return;
    }
    thisObj.closest('form').submit();
  });

  // 页面复制
  var clip = new ZeroClipboard( $('.js_copyList'), {
    moviePath: "../../../../js/zclip/ZeroClipboard.swf"
  });
  clip.on( 'complete', function(client, args) {
    showMsg("复制成功，复制内容为："+ args.text, 1);
  });

  // 显示维修的详细信息
  $('.refund_info').click(function(){
    var id = $(this).closest('tr').data('id');
    var url = siteUrl+'/barter/admin/ajax_refund_info';
    $.post( url, { id:id }, function(data){
        ace.dialog({
          'title':'维修详细信息',
          'content':data,
          'width':1000,
          'cancel':function(){
            $('.aceDialog').hide();
          }
        })
      }
    )
  });

  // 编辑
  $('body').on('click', '.js_editRefundRelation', function(){
    var type = $(this).data('type');
    var that = $(this);
    var relationId = $(this).closest('tr').data('relation_id');
    var refundNum   = $(this).parent().siblings('.js_refundNum');
    var currNum     = parseInt(refundNum.data('refund_num'));
    var currPrice   = parseFloat(refundNum.data('refund_price'));
    var maxNum      = parseInt(refundNum.data('max_num'));
    var url         = siteUrl+'/repair/admin/edit_refund_relation';
    var isModify = $('.js_realTotalPrice').data('is_modify');
    if (type == 'edit') {
      refundNum.html('可退数量: '+maxNum+'<br/><input type="number" value="'+currNum+'">');
      $(this).html('保存');
      $(this).data('type', 'save');
    } else {
      currNum     = refundNum.children('input').val();
      if (currNum == 0 || isNaN(currNum)) currNum = 1;
      if (currNum > maxNum) currNum = maxNum;
      $.post(url, { relation_id:relationId, refund_num:currNum }, function(json){
        if (json.info == 'ok') {
          $('.js_refundTotalPrice').html(json.count);
          refundNum.html(currNum);
          refundNum.data('refund_num', currNum);
          refundNum.next().html(currPrice*currNum);
          that.html('编辑');
          that.data('type', 'edit');
          if (isModify != 1) {
            $('.js_refundTotalPrice_'+json.id).html(json.count);
            $('.js_realTotalPrice').val(json.count);
          }
        } else {
          showMsg(json.data);
        }
      }, 'json');
    }
  });

  // 删除
  $('body').on('click', '.js_deleteRefundRelation', function(){
    var relationId = $(this).closest('tr').data('relation_id');
    var url        = siteUrl+'/repair/admin/delete_refund_relation';
    var that       = $(this);
    var type       = $(this).data('type');
    var isModify = $('.js_realTotalPrice').data('is_modify');
    if (type == 'delete') {
      if (confirm('您确定要删除当前商品？')) {
        $.post(url, { relation_id:relationId, status:0 }, function(json){
          if (json.info == 'ok') {
            that.data('type', 'refresh');
            that.prev().remove();
            that.removeClass('btn-danger').addClass('btn-primary').html('恢复');
            that.parent().prev().children().removeClass('label-primary').addClass('label-default').html('失效');
            $('.js_refundTotalPrice').html(json.count);
            if (isModify != 1) {
              $('.js_refundTotalPrice_'+json.id).html(json.count);
              $('.js_realTotalPrice').val(json.count);
            }
          } else {
            showMsg(json.data);
          }
        }, 'json');
      }
    } else {
      $.post(url, { relation_id:relationId, status:1 }, function(json){
        if (json.info == 'ok') {
          that.data('type', 'delete');
          that.parent().prepend('<span class="btn btn-xs btn-primary js_editRefundRelation" data-type="edit">修改</span>');
          that.removeClass('btn-primary').addClass('btn-danger').html('删除');
          that.parent().prev().children().removeClass('label-default').addClass('label-primary').html('有效');
          $('.js_refundTotalPrice').html(json.count);
          if (isModify != 1) {
            $('.js_refundTotalPrice_'+json.id).html(json.count);
            $('.js_realTotalPrice').val(json.count);
          }
        } else {
          showMsg(json.data);
        }
      }, 'json');
    }
  });

  // 发货
  $('.js_sendExpress').on('click', function(){
    var id = $(this).closest('tr').data('id');
    var url = siteUrl+'/barter/admin/ajax_get_express';
    $.get(url,{  }, function(data){
      ace.dialog({
        'title':'请填写驳回理由',
        'content':data,
        'ok':function(){
          var express = $('.js_expressCode').val();
          var company = $('.js_expressCompany').val();
          var url = siteUrl+'/repair/admin/send_express';
          if (!express) {
            showMsg('快递号不能为空');
            return false;
          }
          $.post(url, { id:id, refund_express:express, express_company:company }, function(json){
            if (json.info == 'ok') {
              location.href = location.href;
            } else {
              showMsg(json.info);
            }
          }, 'json');
        },
        'cancel':function(){
          $('.aceDialog').hide();
        }
      });
    });
  });

  $('.refundOk').click(function(){
    if(!confirm('确定要执行此操作吗?')){
      return false;
    }
    var id = $(this).closest('tr').data('id');
    window.location.href="{AnUrl('repair/admin/change_refund_status?id="+id+"&refund_status=5')}";
  })
  $('.refundOver').click(function(){
    if(!confirm('确定要执行此操作吗?')){
      return false;
    }
    var id = $(this).closest('tr').data('id');
    window.location.href="{AnUrl('repair/admin/change_refund_status?id="+id+"&refund_status=20')}";
  })

  $('.refundCancel').click(function(){
    var id = $(this).closest('tr').data('id');
    ace.dialog({
      'title':'请填写驳回理由',
      'content':'<textarea style="margin:30px;width:550px;height:110px;" class="note"></textarea>',
      'ok':function(){
        var note = $('.note').val();
        if (!note) {
          alert('理由不能为空');
          return false;
        }
        window.location.href="{AnUrl('repair/admin/change_refund_status?id="+id+"&refund_status=30&note="+note+"')}";
      }
    })
  })
});

function refunding(ids)
{
  var url = siteUrl+'/repair/admin/ajax_request_refund';
  var refundIds = ids;
  if (refundIds.length < 1) {
    return;
  }


  var fail = 0;
  for (var i = 0; i < refundIds.length; i++) {
    if (fail == 1) {
      showMsg('批量退款失败');
      return;
    }
    var tr = $('.js_refundTr'+refundIds[i]);
    var id = refundIds[i];
    console.log(id);
    tr.find('span').removeClass('label-warning').addClass('label-info').html('处理中...');
    setTimeout(function(){
      $.ajax({
        url:url,
        async:false,
        data: { id:id },
        dataType:'json',
        success:function(json){
          if (json.info == 'ok') {
            tr.find('span').removeClass('label-info').addClass('label-default').html('已提交');
          } else {
            fail = 1;
          }
        },
        error:function(){
          fail = 1;
        }
      });
    }, 1000);
  }
}

function getIds(obj)
{
  var ids = new Array();
  obj.closest('table').find('.listSelect').each(function(){
    var id = $(this).closest('tr').data('id');
    if ($(this).attr('checked') == 'checked') {
      ids.push(id);
    }
  });

  return ids;
}
</script>
{include_core file="admin/lib/footer.html"}